/* jshint esversion: 6 */
import Vue from "vue";
import App from "./App.vue";


//导入路由组件
import VueRouter from "vue-router";
Vue.use(VueRouter);

//导入vuex
import Vuex from "vuex";
Vue.use(Vuex);
//读取本地存储
var car =JSON.parse(localStorage.getItem("car") || "[]");
const store = new Vuex.Store({
  state:{
    car:car
  },
  mutations:{
    addToCar(state,goodInfo){
      let flag = false;
      state.car.some(item => {
        if(item.id == goodInfo.id){
          flag = true;
          item.count += goodInfo.count;
          return true;
        }
      });

      if(!flag){
        state.car.push(goodInfo);
      }

      localStorage.setItem("car",JSON.stringify(state.car));
    },
    updateTocar(state,goodInfo){
      state.car.some(item => {
        if(item.id === goodInfo.id){
          item.count = goodInfo.count;
          return true;
        }
      });
      localStorage.setItem("car",JSON.stringify(state.car))
    },
    deltoCar(state,id){
      state.car.some((item,i) => {
        if(item.id === id){
          state.car.splice(i,1);
        }
      });
      localStorage.setItem("car",JSON.stringify(state.car));
    },
    updateSelect(state,info){
      state.car.some(item => {
        if(item.id === info.id){
          item.selected = info.selected;
          return true;
        }
      });
      localStorage.setItem("car",JSON.stringify(state.car))
    }
  },
  getters:{
    getCount(state){
      let c = 0;
      state.car.forEach(item => {
        c += item.count;
      });
      return c;
    },
    getSelected(state){
      let obj={};
      state.car.forEach(item => {
        obj[item.id]=item.selected;
      });
      return obj;
    },
    getTotalCountAndTotalprice(state){
      let obj = {
        count:0,
        price:0
      }

      state.car.forEach(item => {
        if(item.selected){
          obj.count += item.count;
          obj.price +=item.price*item.count
        }
      });
      return obj;
    }
  }
})

//导入路由配置文件
import router from './route';

// mui的样式
import "./lib/mui/css/mui.min.css";
import "./lib/mui/css/icons-extra.css"

import 'mint-ui/lib/style.css';
import {Header} from 'mint-ui'; 
Vue.component(Header.name, Header);
import { Swipe, SwipeItem } from 'mint-ui';
Vue.component(Swipe.name, Swipe);
Vue.component(SwipeItem.name, SwipeItem);
import { Button } from 'mint-ui';
Vue.component(Button.name, Button);
import { Lazyload } from 'mint-ui';
Vue.use(Lazyload);

import { Switch } from 'mint-ui';
Vue.component(Switch.name, Switch);


//
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
 
Vue.use(ElementUI);
// Vue.use(Mint);

import VuePreview from 'vue-preview'
 
// defalut install
Vue.use(VuePreview, {
    mainClass: 'preview-img',
    barsSize: {top: 0, bottom: 0},
    captionEl: false,
    fullscreenEl: false,
    shareEl: false,
    bgOpacity: 0.85,
    tapToClose: true,
    tapToToggleControls: false
  })
new Vue({
    el:"#app",
    render: c => c(App),
    router,
    store
});